package com.smart.parking.service.impl;


import com.smart.parking.entity.RepairEntity;
import com.smart.parking.service.RepairService;
import com.smart.parking.service.SpaceService;
import com.smart.parking.utils.PageUtils;
import com.smart.parking.utils.Query;
import com.smart.parking.vo.SpaceVO;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.smart.parking.mapper.SpaceMapper;
import com.smart.parking.entity.SpaceEntity;


@Service("spaceService")
public class SpaceServiceImpl extends ServiceImpl<SpaceMapper, SpaceEntity> implements SpaceService {

    @Autowired
    private Mapper dozerMapper;

    @Autowired
    private RepairService repairService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpaceEntity> page = this.page(
                new Query<SpaceEntity>().getPage(params),
                new QueryWrapper<SpaceEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 车位停车
     *
     * @param spaceId 车位id
     */
    @Override
    public void lotParking(Long spaceId) {
        SpaceEntity spaceEntity = baseMapper.selectById(spaceId);
        //更改为不空闲
        spaceEntity.setSpaceFree(1);
        baseMapper.updateById(spaceEntity);
    }

    /**
     * 释放车位
     *
     * @param spaceId 车位id
     */
    @Override
    public void releaseParking(Long spaceId) {
        SpaceEntity spaceEntity = baseMapper.selectById(spaceId);
        //更改为空闲
        spaceEntity.setSpaceFree(0);
        baseMapper.updateById(spaceEntity);
    }

    /**
     * 报修车位
     *
     * @param spaceId 车位id
     */
    @Override
    public boolean repairParking(Long spaceId) {
        //报修 新增一条维修记录
        SpaceEntity spaceEntity = baseMapper.selectById(spaceId);
        //更改为损坏
        spaceEntity.setSpaceDamage(1);
        int update = baseMapper.updateById(spaceEntity);
        RepairEntity repairEntity = new RepairEntity();
        repairEntity.setSpaceId(spaceId);
        repairService.addRepair(repairEntity);
        return update > 0;
    }

    /**
     * 完成车位恢复
     *
     * @param spaceId 车位id
     */
    @Override
    public void completeParking(Long spaceId) {
        //修复成功
        SpaceEntity spaceEntity = baseMapper.selectById(spaceId);
        //更改为可用
        spaceEntity.setSpaceDamage(0);
        baseMapper.updateById(spaceEntity);
    }

    /**
     * 添加车位
     *
     * @param spaceVO 空间签证官
     */
    @Override
    public void addSpace(SpaceVO spaceVO) {
        SpaceEntity spaceEntity = dozerMapper.map(spaceVO, SpaceEntity.class);
        baseMapper.insert(spaceEntity);
    }

    /**
     * 增加车位
     *
     * @param spaceVOList 空间volist
     */
    @Override
    public void addSpaceList(List<SpaceVO> spaceVOList) {
        spaceVOList.forEach(this::addSpace);
    }

    /**
     * 更新车位
     *
     * @param spaceEntity 空间实体
     */
    @Override
    public void updateSpace(SpaceEntity spaceEntity) {
        baseMapper.updateById(spaceEntity);
    }

    /**
     * 删除车位
     *
     * @param spaceId 空间id
     */
    @Override
    public void deleteSpace(Long spaceId) {
        baseMapper.deleteById(spaceId);
    }

    /**
     * 批量删除车位
     *
     * @param spaceIds 空间id
     */
    @Override
    public void deleteSpaces(Long[] spaceIds) {
        for (Long spaceId : spaceIds) {
            this.deleteSpace(spaceId);
        }
    }

    /**
     * 修改维修状态
     */
    @Override
    public void updateRepair(Long repairID, Integer state) {
        if (state == 3){
            //维修完成
            RepairEntity repairEntity = repairService.getById(repairID);
            repairEntity.setRepairState(state);
            repairService.updateById(repairEntity);
            this.completeParking(repairEntity.getSpaceId());
        }else {
            RepairEntity repairEntity = repairService.getById(repairID);
            repairEntity.setRepairState(state);
            repairService.updateById(repairEntity);
        }
    }

}
